"use strict";
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from "./store";
import router from './router'
import mixin from '@/mixins/common'
import ViewImg from '@/components/ViewImg'
import ElementUI from "element-ui";
import jquery from 'jquery'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
    
import 'element-ui/lib/theme-chalk/index.css';
import '@/assets/styles/index.scss' // 全局css样式
import { getCookie, setCookie, getURLQueryString } from '@/utils/common'

import '@/assets/iconfont/iconfont.css' //阿里字体图标
import '@/utils/addQuery' // 设置全局用 query

import 'babel-polyfill' //兼容360浏览器兼容模式

if(getURLQueryString('token')) {
	setCookie('token', getURLQueryString('token'))
}
if(!sessionStorage.getItem('producttype')) {
//	setCookie('producttype', 'ring')
	sessionStorage.setItem('producttype', 'ring')
}

import requestGlobal from "@/api";

Vue.component('ViewImg', ViewImg)
Vue.mixin(mixin)
Vue.use(ElementUI, {
	size: "small"
});
Vue.use(VueAwesomeSwiper)
Vue.prototype.msgSuccess = function(msg) {
	this.$message({
		showClose: true,
		message: msg,
		type: "success"
	});
}

Vue.prototype.msgError = function(msg) {
	this.$message({
		showClose: true,
		message: msg,
		type: "error"
	});
}

Vue.prototype.msgInfo = function(msg) {
	this.$message.info(msg);
}
Vue.prototype.$jquery=jquery
Vue.prototype.$request = requestGlobal;

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
	el: '#app',
	router,
	store,
	components: {
		App
	},
	template: '<App/>',
	mounted() { //页面刷新防止vuex数据丢失
		window.addEventListener("beforeunload", () => {
			//如果是点击了退出登录则不再缓存数据
			if(!localStorage.getItem('logout')){
				localStorage.setItem("stateInfo", JSON.stringify(this.$store.state));
			}
			localStorage.removeItem('logout')
		});
		//老平台切换账号时，新平台刷新token
		window.addEventListener("storage", () => {
			const query = Vue.prototype.$query
			let token = localStorage.getItem('token')
			localStorage.removeItem("token");
			if(token&&token!=getCookie('token')){
				var searchStr = ''
				query.token=token
				for(var key in query){
					searchStr+=(searchStr?'&':'')+key+'='+query[key]
				}
				window.location.href=window.location.href.replace(window.location.search,'?'+searchStr)
			}
		});
		if(localStorage.getItem("stateInfo")) {
			this.$store.replaceState(
				Object.assign({},
					this.$store.state,
					JSON.parse(localStorage.getItem("stateInfo"))
				)
			);
		}
		localStorage.removeItem("stateInfo");
	}
})